//
//  ViewController.m
//  QQ聊天
//
//  Created by song on 15/1/14.
//  Copyright (c) 2015年 song. All rights reserved.
//

#import "ViewController.h"
#import "JOMessage.h"
#import "JOMessageCell.h"
@interface ViewController () <UITableViewDataSource ,UITableViewDelegate,UITextFieldDelegate>



@end

@implementation ViewController




-(NSMutableArray *)messages
{
    if(_messages == nil)
    {
        NSArray *dicarray =[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"messages.plist" ofType:nil]];
        
        NSMutableArray *msgarray = [NSMutableArray array];
        
        for(NSDictionary *dict  in dicarray)
        {
            JOMessage *message = [JOMessage messageWithDict:dict];
            JOMessageFrame *messageFrame = [[JOMessageFrame alloc]init];
            messageFrame.message = message;
            [msgarray addObject:messageFrame];
        }
        _messages = msgarray;
        
    }
    
    return _messages;
}




-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.messages.count;
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JOMessageCell *cell = [JOMessageCell cellWithTableView:tableView];
    cell.messageFrame = self.messages[indexPath.row];
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JOMessageFrame *frame = _messages[indexPath.row];
    return frame.cellHeight;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    // 设置无分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor =  [UIColor colorWithRed:224/255.0  green:224/255.0 blue:224/255.0 alpha:1.0];
    // 取消选中
    self.tableView.allowsSelection =NO;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
    UIView  *leftview = [[UIView alloc]initWithFrame:CGRectMake(0,0,20,0)];
    leftview.backgroundColor =[UIColor redColor];
    self.inputTextview.leftView = leftview;
    self.inputTextview.leftViewMode =UITextFieldViewModeAlways;
    self.inputTextview.delegate =self;
    
}


-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}


/**
 * 惦记return 按钮的时候回调用
 *
 **/
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"%@",textField.text);
    return YES;
}

-(void)keyboardWillChangeFrame:(NSNotification *)noti
{
    CGFloat duration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    
//    self.view.transform = CGAffineTransformMakeTranslation(0, -315);
    CGRect keyrect = [noti.userInfo[UIKeyboardFrameEndUserInfoKey]CGRectValue];
    CGFloat transformY = keyrect.origin.y -self.view.frame.size.height;

                      
    [UIView animateWithDuration:duration animations:^{
         self.view.superview.transform =CGAffineTransformMakeTranslation(0, transformY);
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

}

-(void) dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}


@end
